package com.cqndt.disaster.device.common.shrio;

import java.util.Map;
import javax.servlet.Filter;
import java.util.LinkedHashMap;
import org.apache.shiro.mgt.SecurityManager;
import org.springframework.context.annotation.Bean;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Configuration;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;

/**
 * @author lgb
 * @date 2020-8-31
 * @说明 Shiro的配置文件
 **/
@Configuration
public class ShiroConfig {

    @Bean("sessionManager")
    public SessionManager sessionManager(){
        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
        //设置session过期时间为1小时(单位：毫秒)，默认为30分钟
        sessionManager.setGlobalSessionTimeout(60 * 60 * 1000);
        sessionManager.setSessionValidationSchedulerEnabled(true);
        sessionManager.setSessionIdUrlRewritingEnabled(false);
        return sessionManager;
    }

    @Bean("securityManager")
    public SecurityManager securityManager(SessionManager sessionManager) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(getUserRealm());
        securityManager.setSessionManager(sessionManager);
        return securityManager;
    }

    @Bean("shiroFilter")
    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        shiroFilter.setSecurityManager(securityManager);
        shiroFilter.setLoginUrl("/login.html");
        shiroFilter.setUnauthorizedUrl("/");

        Map<String, Filter> filters = new LinkedHashMap<String, Filter>();
        filters.put("token", new LoginAuthorizationFilter());
        filters.put("corsFilter", new RestFilter());
        shiroFilter.setFilters(filters);

        Map<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put("/swagger/**", "anon");
        filterMap.put("/v2/api-docs", "anon");
        filterMap.put("/swagger-ui.html", "anon");
        filterMap.put("/webjars/**", "anon");
        filterMap.put("/swagger-resources/**", "anon");
        filterMap.put("/tabDevicePatrol/getCheckExportExcel", "anon");
        filterMap.put("/tabDevicePatrol/removeCheckById", "anon");
        filterMap.put("/tabDevicePatrol/importExcel", "anon");
        filterMap.put("/tabDevicePatrol/uploadDeviceImg", "anon");
        filterMap.put("/tabDevicePatrol/removeDeviceImgById", "anon");
        filterMap.put("/tabDevicePatrol/getEchartExport", "anon");
        filterMap.put("/tabDevicePatrol/downLoadDeviceImg", "anon");
        filterMap.put("/tabBasic/uploadBasic", "anon");
        filterMap.put("/project/uploadProjectImg", "anon");
        filterMap.put("/login/noLogin", "anon");
        filterMap.put("/login/getLoginToken", "anon");
        filterMap.put("/data", "anon");
        filterMap.put("/upload/**", "anon");
        filterMap.put("/statics/**", "anon");
        filterMap.put("/login/login", "anon");
        filterMap.put("/favicon.ico", "anon");
        filterMap.put("/login/getCaptcha", "anon");
        filterMap.put("/login/checkCaptcha", "anon");
        filterMap.put("/login/logout", "anon");
        filterMap.put("/login/getKey", "anon");
        filterMap.put("/rain/upload", "anon");
        filterMap.put("/websocket/**","anon");
        filterMap.put("/rainGauge", "anon");
        filterMap.put("/tabVideo/getTabVideoMonitoring", "anon");
        shiroFilter.setFilterChainDefinitionMap(filterMap);
        return shiroFilter;
    }

    @Bean("lifecycleBeanPostProcessor")
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }
    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator proxyCreator = new DefaultAdvisorAutoProxyCreator();
        proxyCreator.setProxyTargetClass(true);
        return proxyCreator;
    }
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }
    @Bean
    public UserRealm getUserRealm(){
        return new UserRealm();
    }
}